package com.javasm.mb_store_api_final.giftsandgoods.gift.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.javasm.mb_store_api_final.giftsandgoods.gift.entity.GmGift;
import com.javasm.mb_store_api_final.giftsandgoods.gift.vo.GiftQueryVo;
import com.javasm.mb_store_api_final.giftsandgoods.gift.vo.GiftVo;
import com.javasm.mb_store_api_final.giftsandgoods.goods.entity.GgGoods;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * (GmGift)表数据库访问层
 *
 * @author makejava
 * @since 2024-07-29 19:17:54
 */
public interface GmGiftDao extends BaseMapper<GmGift> {

    @Select("<script>"+
            "SELECT gf.id, gd.gname gift_name, gt.type_name gift_type ,gd.min_stock, " +
            "gd.market_price,gd.purchase_price   " +
            "from gm_gift gf, gg_goods gd,gg_goods_type gt " +
            "WHERE gf.goods_id = gd.id and gt.id =gd.gtype "
            + "</script>"
            )

    List<GiftVo> listGifts();

    @Select("SELECT  DISTINCT gcolor from gg_goods gd,gm_gift gf WHERE gf.goods_id = gd.id ")
    List<String> listgcolor();

    @Select("SELECT  DISTINCT gt.type_name from gg_goods gd,gg_goods_type gt,gm_gift gf\n" +
            "WHERE gt.id =gd.gtype and goods_id = gd.id ")
    List<String> listgtype();

    @Select("SELECT  DISTINCT gbrand from gg_goods gd,gm_gift gf WHERE gf.goods_id = gd.id ")
    List<String> listgbrand();

    @Select("<script>"
            + "SELECT gf.id, gd.gname gift_name, gt.type_name gift_type ,gd.min_stock, gd.market_price,gd.purchase_price   from gg_goods gd, gm_gift gf,gg_goods_type gt WHERE gf.goods_id = gd.id and gt.id =gd.gtype "
            + "<if test='gbrand != null and gbrand.trim().length() > 0'>"
            + "AND gd.gbrand = #{gbrand} "
            + "</if>"
            + "<if test='gcolor != null'>"
            + "AND gd.gcolor = #{gcolor} "
            + "</if>"
            + "<if test='giftName != null and giftName.trim().length() > 0'>"
            + "AND gd.gname LIKE CONCAT('%', #{giftName}, '%') "
            + "</if>"
            + "<if test='gtype != null'>"
            + "AND gt.type_name = #{gtype} "
            + "</if>"
            + "<if test='gmodel != null and gmodel.trim().length() > 0'>"
            + "AND gd.gmodel LIKE CONCAT('%', #{gmodel}, '%') "
            + "</if>"
            + "<if test='pageNum != null'>"
            + " LIMIT #{pageNum} ,#{pageSize} "
            + "</if>"
            + "</script>"
    )
    List<GiftVo> ListByParams(GiftQueryVo giftQueryVo);


    @Select("SELECT gg_goods.* FROM gg_goods ,gm_gift WHERE gg_goods.id =gm_gift.goods_id and gm_gift.id =#{id}")
    GgGoods listDetail(@Param("id") Integer id);


    @Update("UPDATE gm_gift \n" +
            "SET update_time = NOW() \n" +
            "WHERE id =#{id};")
    Integer updateGift(GiftVo giftVo);

    @Update("UPDATE gg_goods \n" +
            "SET gname = #{giftName}, \n" +
            "  min_stock = #{minStock}, market_price = #{marketPrice}, \n" +
            "    purchase_price = #{purchasePrice}, update_time = NOW() \n" +
            "WHERE id = (SELECT goods_id FROM gm_gift WHERE id = #{id});")
    Integer updateGoodsById(GiftVo giftVo);
}

